package cn.admin.spring.datasource;

import cn.admin.config.mysql.MySql;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.SQLException;

/**
 * @Author: 毛苏宇
 * @Description: 数据源配置, 包含事务
 * @Date: Create in 11:03 2019/4/25
 */
@Configuration
@MapperScan(value = "cn.admin.dao") //扫描dao层mapper
@EnableTransactionManagement()  //开启事务
public class DataSourceConfig {


    //分页插件
    @Autowired
    private PageInterceptor pageInterceptor;
    //通配符查找资源
    @Autowired
    private PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver;


    /**
     * 数据源
     * initMethod = "init"方法执行前应该做的一些事情
     * destroyMethod = "close"方法执行完成后的收尾工作
     *
     * @return
     * @throws Exception
     */
    @Bean(initMethod = "init", destroyMethod = "close")
    public DruidDataSource druidDataSource() throws SQLException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(MySql.MYSQL_URL);
        druidDataSource.setUsername(MySql.MYSQL_USER_NAME);
        druidDataSource.setPassword(MySql.MYSQL_USER_PASSWORD);
        druidDataSource.setFilters(MySql.MYSQL_FILTERS);
        druidDataSource.setMaxActive(MySql.MYSQL_MAX_ACTIVE);
        druidDataSource.setInitialSize(MySql.MYSQL_INITIAL_SIZE);
        druidDataSource.setMaxWait(MySql.MYSQL_MAX_WAIT);
        druidDataSource.setMinIdle(MySql.MYSQL_MIN_IDLE);
        druidDataSource.setTimeBetweenEvictionRunsMillis(MySql.MYSQL_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
        druidDataSource.setMinEvictableIdleTimeMillis(MySql.MYSQL_MIN_EVICTABLE_IDLE_TIME_MILLIS);
        druidDataSource.setValidationQuery(MySql.MYSQL_VALIDATION_QUERY);
        druidDataSource.setTestWhileIdle(MySql.MYSQL_TEST_WHILE_IDLE);
        druidDataSource.setTestOnBorrow(MySql.MYSQL_TEST_ON_BORROW);
        druidDataSource.setTestOnReturn(MySql.MYSQL_TEST_ON_RETURN);
        druidDataSource.setPoolPreparedStatements(MySql.MYSQL_POOL_PREPARED_STATEMENTS);
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(MySql.MYSQL_MAX_POOL_PREPARED_STATEMENT_PER_CONNECTION_SIZE);
        return druidDataSource;
    }

    /**
     * 事务管理
     *
     * @return
     * @throws Exception
     */
    @Bean
    public DataSourceTransactionManager transactionManager() throws Exception {
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(druidDataSource());
        return dataSourceTransactionManager;
    }


    @Bean
    public SqlSessionFactoryBean sqlSessionFactory() throws Exception {
        //得到mappers下的所有mapper文件
        Resource[] resources = pathMatchingResourcePatternResolver.getResources("/mappers/*.xml");
        //MyBatis插件
        Interceptor[] interceptors = {
                pageInterceptor
        };
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        //添加数据源
        sqlSessionFactoryBean.setDataSource(druidDataSource());
        //dao层xml文件
        sqlSessionFactoryBean.setMapperLocations(resources);
        //添加插件
        sqlSessionFactoryBean.setPlugins(interceptors);
        //扫描实体类
        sqlSessionFactoryBean.setTypeAliasesPackage("cn.admin.entity");
        return sqlSessionFactoryBean;
    }


}
